/**
 *  testTrafficLight
 *  Author: thuanvd_2
 *  Description: chu y khoi tao gia tri
 */

model testTrafficLight

global {
	/** Insert the global definitions, variables and actions here */
	file shape_file_traffic_light <- file('../includes/crossroads/traffic_light3.shp');
	file shape_file_bounds <- file('../includes/crossroads/bounds.shp');
	
	int time_start_1 <- 0;
	int time_start_2 <- 50;
	int time_start_3 <- 0;
	int time_start_4 <- 50;
	
//	int time_yellow_1 <- 2;
//	int time_yellow_2 <- 2;
//	int time_yellow_3 <- 2;
//	int time_yellow_4 <- 2;

	int time_green_1 <- 50;
	int time_green_2 <- 50;
	int time_green_3 <- 50;
	int time_green_4 <- 50;
	
	int time_yellow_default <- 2;
	
	int time_red_1 <- 48;
	int time_red_2 <- 48;
	int time_red_3 <- 48;
	int time_red_4 <- 48;
	
	list list_time_start <- [time_start_1, time_start_2, time_start_3, time_start_4];
	list list_time_green <- [time_green_1, time_green_2, time_green_3, time_green_4];
	list list_time_yellow <-[time_yellow_default,	time_yellow_default,	time_yellow_default,	time_yellow_default];
	list list_time_red <-   [time_red_1,   time_red_2,   time_red_3,   time_red_4];
	init {
		create traffic_light 	from: shape_file_traffic_light
				with:[id_light::read('id')]{
					set time_start <- list_time_start at id_light ;
					set time_green <- list_time_green at id_light ;
					set time_red   <- list_time_red   at id_light ;
					set time_yellow <- list_time_yellow at id_light ;
				}
	}
}

environment bounds: shape_file_bounds;

entities {
	/** Insert here the definition of the species of agents */
	species traffic_light control:fsm{
//		khoi tao the nay khong on 
		int id_light <- 0;
		int time_start <- list_time_start at id_light;
		int time_red <- list_time_red at id_light;
		int time_green <- list_time_green at id_light;
		int time_yellow <-list_time_yellow at id_light;
		int total_time <- time_red + time_green + time_yellow;
		int cycle_time update:(time - time_start >= 0)?
			(time - time_start) mod (total_time)
			:(total_time - (time_start - time));
			
		// xem xet trong truong hop chuyen len init
		 
		rgb color <- rgb('red');
		
		state startup initial:true{
//			set color <- rgb('red');
			transition to: in_red when: cycle_time >= time_green + time_yellow and cycle_time < total_time;
			transition to: in_green when: cycle_time >= 0 and cycle_time < time_green;
			transition to: in_yellow when: cycle_time >= time_green and cycle_time < time_green + time_yellow;
		}
		
		state in_green {
			set color <- rgb('green');
			transition to: in_yellow when: cycle_time = time_green;
		}
		
		state in_yellow {
			set color <- rgb('yellow');
			transition to: in_red when: cycle_time = time_green + time_yellow;
		}
		
		state in_red {
			set color <- rgb('red');
			transition to: in_green when: cycle_time =0;
		}
		
		aspect base {
			draw geometry: shape color: color ;
			draw text: string(cycle_time) at: location + {-3,1.5} color: rgb('black') size: 10.8 ;
			
		}
	}
	
}

experiment testTrafficLight type: gui {
	output{
		display crossroads refresh_every:1{
			species traffic_light aspect:base;	
		}
	}
}
